const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    actionText: '查询',
    userList: [],
    total: 0,    //分页总数
    page: 1,    //分页记录数
    pageSize: 10,   //分页大小
    onRefresh: true,
    item: {}
  },

  //点击了数据行按钮
  editHandleClick: function (e) {
    let item = e.currentTarget.dataset.entity;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      user: item
    })
    wx.navigateBack({
      detail: 1
    })
  },
  //搜索栏内容变化
  changeHandle: function (e) {
    this.setData({
      value: e.detail.value
    })
  },
  //搜索栏清除
  clearHandle: function (e) {
    console.log('clearHandle' + e.detail.value)
    this.setData({
      value: ''
    });
  },
  //搜索栏查询
  actionHandle: function () {
    this.setData({
      onRefresh: false,
      page: 1
    });
    this.getMyMemberFunc();
  },
  //查询会员列表
  getMyMemberFunc: function () {
    let that = this;
    var onRefresh = that.data.onRefresh;//false为重新刷新数据，true为分页累加数据
    let obj = {
      method: 'POST',
      showLoading: false,
      url: 'user/getMyShopUser',
      data: {
        'nickName': that.data.value,
        'page': that.data.page,
        'size': that.data.pageSize,
        'column': 'createTime',
        'order': 'asc'
      }
    }
    httpUtils.request(obj).then(res => {
      if (res.data.retCode == 0) {
        var datas = res.data.data.records;
        if (datas != null && datas.length > 0) {
          if (onRefresh) {//false为重新刷新数据，true为分页累加数据
            that.setData({
              userList: that.data.userList.concat(datas),
              page: res.data.data.current,
              total: res.data.data.pages
            })
          } else {
            that.setData({
              userList: datas,
              page: res.data.data.current,
              total: res.data.data.pages
            })
          }
        } else {
          if (!onRefresh) {
            that.setData({
              userList: []
            })
          }
          ui.showToast('没有更多数据了')
        }
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      onRefresh: false,
      page: 1,
      userList: [],
    });
    this.getMyMemberFunc();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      onRefresh: false,
      page: 1,
      userList: [],
    });
    this.getMyMemberFunc();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let that = this;
    this.setData({
      onRefresh: true,
      page: that.data.page + 1
    });
    this.getMyMemberFunc();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})